package pers.mine.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.mine.dao.GoodsGroupMapper;
import pers.mine.dao.QueryMapper;
import pers.mine.dto.PageInfo;
import pers.mine.dto.QueryHandler;
import pers.mine.po.GoodsGroup;
import pers.mine.service.inter.GoodsGroupService;

/**
 * @author Mine
 * @since 2016年11月27日_下午8:54:15
 */
@Service
public class GoodsGroupServiceImpl implements GoodsGroupService {
	@Autowired
	private GoodsGroupMapper ggm;
	@Autowired
	private QueryMapper qm;
	// 日志记录器
	private static final Log logger = LogFactory
			.getLog(GoodsGroupServiceImpl.class);

	@Override
	public boolean addGoodsGroup(GoodsGroup one) {
		// 判断参数是否为空
		if (one == null) {
			logger.debug("添加新商品组:传入参数为空...");
			return false;
		}
		// id设为null
		one.setId(null);
		// 状态设置为1
		one.setState(1);
		// 执行添加
		try {
			// 使用动态插入语句
			if (ggm.insertSelective(one) == 1) {
				if (logger.isDebugEnabled()) {
					logger.debug("添加新商品组:成功!");
				}
				return true;
			}
			if (logger.isDebugEnabled()) {
				logger.debug("添加新商品组:失败!非法的影响行数...");
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("添加新商品组:插入发生异常...");
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	public boolean editGoodsGroup(GoodsGroup one) {
		// 判断参数是否为空
		if (one == null) {
			logger.debug("修改商品组:传入参数为空...");
			return false;
		}
		// 不修改状态
		one.setState(null);
		return updateGoodsGroup(one);
	}

	@Override
	public boolean delGoodsGroup(Integer id) {
		// 判断参数是否为空
		if (id == null) {
			logger.debug("删除商品组:传入参数为空...");
			return false;
		}
		GoodsGroup one = new GoodsGroup();
		one.setId(id);
		one.setState(0);
		return updateGoodsGroup(one);
	}

	@Override
	public PageInfo<GoodsGroup> findByQueryHandler(QueryHandler qh) {
		List<GoodsGroup> list = qm.queryGoodsGroup(qh);
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (qh == null || qh.getLimitSentence() == null) {// 没分页
			if (logger.isDebugEnabled()) {
				logger.debug("构造PageInfo:未指定分页信息...");
			}
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = qh.getPageNumber();
			pageSize = qh.getPageSize();
			// 获取总记录数
			rootSize = qm.findLastRows();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("构造PageInfo:rootSize=" + rootSize + ",nowPage="
					+ nowPage + ",pageSize=" + pageSize);
		}
		PageInfo<GoodsGroup> pi = new PageInfo<GoodsGroup>((int) rootSize,
				nowPage, pageSize);
		pi.setBasePageList(list);
		return pi;
	}

	@Override
	public List<GoodsGroup> findBySimpleType() {
		QueryHandler qh = QueryHandler.creat()
				.andColumnMark("gg_state", "=", "1")// 状态必须为1
				.andColumnMark("gg_type", "=", "1");
		qh.set();
		return qm.queryGoodsGroup(qh);
	}

	@Override
	public List<GoodsGroup> findByCarType() {
		QueryHandler qh = QueryHandler.creat()
				.andColumnMark("gg_state", "=", "1")// 状态必须为1
				.andColumnMark("gg_type", "=", "2");
		qh.set();
		return qm.queryGoodsGroup(qh);
	}

	@Override
	public GoodsGroup findOneById(Integer id) {
		// 判断参数是否为空
		if (id == null) {
			logger.debug("删除商品组:传入参数为空...");
			return null;
		}
		return ggm.selectByPrimaryKey(id);
	}

	/**
	 * 更新方法
	 * 
	 * @param one
	 * @return
	 */
	private boolean updateGoodsGroup(GoodsGroup one) {
		// 使用动态更新语句
		try {
			if (ggm.updateByPrimaryKeySelective(one) == 1) {
				return true;
			}
			if (logger.isDebugEnabled()) {
				logger.debug("修改商品组:失败!非法的影响行数...");
			}
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("修改商品组:修改发生异常...");
				e.printStackTrace();
			}
		}
		return false;
	}

}
